using System;
using System.Collections.Generic;
using System.Text;

namespace Placid.Core
{
    public class PTreeNodesMeasure : IDisposable
    {
        public enum MeasureType
        {
            COUNT,
            SUM
        }

        private static PTreeNodesMeasure _empty;

        private string _hNodePath;
        private string _vNodePath;
        private MeasureType _type;
        private List<object> _values;

        static PTreeNodesMeasure()
        {
            _empty = new PTreeNodesMeasure("", "", MeasureType.COUNT);
        }

        public PTreeNodesMeasure(string hNodePath, string vNodePath, MeasureType type)
        {
            Init(hNodePath, vNodePath, type);
        }

        public PTreeNodesMeasure(PTreeNode hNode, PTreeNode vNode, MeasureType type)
        {
            if (hNode != null && vNode != null)
            {
                Init(hNode.Path, vNode.Path, type);
            }
        }

        private void Init(string hNodePath, string vNodePath, MeasureType type)
        {
            _hNodePath = hNodePath;
            _vNodePath = vNodePath;
            _type = type;
            _values = new List<object>();
        }

        public void AddMValue(object value)
        {
            if (this != _empty) _values.Add(value);
        }

        public object Value
        {
            get
            {
                if (this != _empty)
                {
                    bool canSum = true;
                    decimal sum = 0;

                    switch (_type)
                    {
                        case MeasureType.SUM:

                            foreach (object value in _values)
                            {
                                if (value is decimal || value is double || value is int || value is float || value is long || value is Single || value is short)
                                {
                                    sum += Convert.ToDecimal(value);
                                }
                                else
                                {
                                    canSum = false;
                                    break;
                                }
                            }

                            if (canSum)
                            {
                                return sum;
                            }
                            else
                            {
                                return "#N/A!";
                            }

                            break;

                        case MeasureType.COUNT:
                        default:
                            return _values.Count;
                    }
                }
                else
                {
                    return "";
                }
            }
        }

        public static PTreeNodesMeasure Empty
        {
            get { return PTreeNodesMeasure._empty; }
        }

        #region IDisposable Members

        public void Dispose()
        {
            _hNodePath = "";
            _vNodePath = "";
            _values.Clear();
        }

        #endregion
    }
}
